import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { DSLParser } from '../../../packages/dsl-parser/src/parser.js';
import { DomainGraph } from '../../../packages/runtime-core/src/domainGraph.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

/**
 * Main function to process DSL files and generate domain objects
 */
export async function main(dslFilePath?: string): Promise<DomainGraph> {
  // Default DSL file path if not provided
  const defaultDslPath = join(__dirname, '../../../examples/sample-domain.yaml');
  const filePath = dslFilePath || defaultDslPath;
  
  try {
    // Read the DSL file
    const dslContent = readFileSync(filePath, 'utf-8');
    
    // Parse the DSL content
    const parsedDSL = DSLParser.parseYAML(dslContent);
    
    // Create domain graph from parsed DSL
    const domainGraph = DomainGraph.fromDomainModels([parsedDSL]);
    
    console.log('✅ DSL processing completed successfully');
    console.log(`📁 Processed file: ${filePath}`);
    console.log(`🏗️  Generated ${domainGraph.getAllNodes().length} entities`);
    
    return domainGraph;
  } catch (error) {
    console.error('❌ Error processing DSL file:', error);
    throw error;
  }
}

// Export for use in other modules
export { DSLParser, DomainGraph };